//
//  ActivityCollectionViewController.swift
//  甜言蜜语
//
//  Created by 王剑伟 on 16/6/16.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class ActivityCollectionViewController: UICollectionViewController {

    let sectionInsets = UIEdgeInsets(top:6, left: 6, bottom: 6, right: 6)
    
    var modelList: [ActivityModel]? {
        didSet {
            self.collectionView?.reloadData()
        }
    }
    var page = 0
    override func viewDidLoad() {
        super.viewDidLoad()


        self.tabBarController?.tabBar.tintColor = NAVIGATIONBAR_RED_COLOR
        self.collectionViewLayout.collectionView?.backgroundColor = UIColor.whiteColor()
        
        let cellNib = UINib(nibName: "ActivityCollectionViewCell", bundle: nil)
        self.collectionView!.registerNib(cellNib, forCellWithReuseIdentifier: "ActivityCollectionViewCell")
        
        self.collectionView!.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(ActivityCollectionViewController.requestInfo))
        self.collectionView!.mj_footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(ActivityCollectionViewController.nextRequestInfo))
        self.collectionView!.mj_header.beginRefreshing()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        if ApplicationDelegate.isLogin(){
            let phone = publicMethod.userName()
            let pass = publicMethod.passWord()
            let token = publicMethod.token()
            DataTool.login(phone, pass: pass, token: token, completionHandler: { (newModel) in
                guard let newDataes = newModel else {
                    return
                }
                print(newDataes)
                
            })
            
        }
        self.tabBarController?.tabBar.hidden = false
    }
    //MARK:networking
    func requestInfo(){
        self.page = 0
        DataTool.loadActivityData { (modelArray) -> Void in
            guard modelArray != nil else {
                return
            }
                self.collectionView?.mj_header.endRefreshing()
                self.modelList = modelArray
        }

    }
    
    func nextRequestInfo(){
        self.page += 1
        let urlStr = String(format: "%@activity/dosel/fenye/\(self.page)",BaseUrl)
        DataTool.loadNextActivityData(String(self.page), urlStr: urlStr) { (modelArray) -> Void in
             self.collectionView?.mj_footer.endRefreshing()
            guard modelArray != nil else {
                return
            }
           self.modelList! += modelArray!
            
        }

    }

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        var count: Int = 0
        
        if self.modelList != nil {
            count = self.modelList!.count
        }
        
        return count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ActivityCollectionViewCell", forIndexPath: indexPath) as! ActivityCollectionViewCell
    
        // Configure the cell
        cell.model = self.modelList?[indexPath.row]
    
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
    {
        let frame  = self.view.frame;
        var width = frame.width
        
        
        if (frame.width > frame.height) {
            width = frame.height
        }
        width = CGFloat(Int((width-18)/2))
        // println("width....\(width)")
        return CGSize(width: width, height: 303)
        
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat{
        return 6
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
    {
        return 6
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        return sectionInsets
    }

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("ActivityDetailViewController") as! ActivityDetailViewController
        vc.id = modelList![indexPath.row].id
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
